<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>asynctestcase_async_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_asynctestcase_async_test.js.html">asynctestcase_async_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.AsyncTestCaseAsyncTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.AsyncTestCaseAsyncTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>// Has the setUp() function been called.
<a name="line22"></a>var setUpCalled = false;
<a name="line23"></a>// Has the current test function completed. This helps us to ensure that
<a name="line24"></a>// the next test is not started before the previous completed.
<a name="line25"></a>var curTestIsDone = true;
<a name="line26"></a>// Use an asynchronous test runner for our tests.
<a name="line27"></a>var asyncTestCase =
<a name="line28"></a>    goog.testing.AsyncTestCase.createAndInstall(document.title);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Uses window.setTimeout() to perform asynchronous behaviour and uses
<a name="line33"></a> * asyncTestCase.waitForAsync() and asyncTestCase.continueTesting() to mark
<a name="line34"></a> * the beginning and end of it.
<a name="line35"></a> * @param {number} numAsyncCalls The number of asynchronous calls to make.
<a name="line36"></a> * @param {string} name The name of the current step.
<a name="line37"></a> */
<a name="line38"></a>function doAsyncStuff(numAsyncCalls, name) {
<a name="line39"></a>  if (numAsyncCalls &gt; 0) {
<a name="line40"></a>    curTestIsDone = false;
<a name="line41"></a>    asyncTestCase.waitForAsync(
<a name="line42"></a>        &#39;doAsyncStuff-&#39; + name + &#39;(&#39; + numAsyncCalls + &#39;)&#39;);
<a name="line43"></a>    window.setTimeout(function() {
<a name="line44"></a>      doAsyncStuff(numAsyncCalls - 1, name);
<a name="line45"></a>    }, 0);
<a name="line46"></a>  } else {
<a name="line47"></a>    curTestIsDone = true;
<a name="line48"></a>    asyncTestCase.continueTesting();
<a name="line49"></a>  }
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>function setUpPage() {
<a name="line53"></a>  debug(&#39;setUpPage was called.&#39;);
<a name="line54"></a>  doAsyncStuff(3, &#39;setUpPage&#39;);
<a name="line55"></a>}
<a name="line56"></a>function setUp() {
<a name="line57"></a>  assertTrue(curTestIsDone);
<a name="line58"></a>  doAsyncStuff(3, &#39;setUp&#39;);
<a name="line59"></a>}
<a name="line60"></a>function tearDown() {
<a name="line61"></a>  assertTrue(curTestIsDone);
<a name="line62"></a>}
<a name="line63"></a>function test1() {
<a name="line64"></a>  assertTrue(curTestIsDone);
<a name="line65"></a>  doAsyncStuff(1, &#39;test1&#39;);
<a name="line66"></a>}
<a name="line67"></a>function test2_asyncContinueThenWait() {
<a name="line68"></a>  var activeTest = asyncTestCase.activeTest_;
<a name="line69"></a>  function async1() {
<a name="line70"></a>    asyncTestCase.continueTesting();
<a name="line71"></a>    asyncTestCase.waitForAsync(&#39;2&#39;);
<a name="line72"></a>    window.setTimeout(async2, 0);
<a name="line73"></a>  }
<a name="line74"></a>  function async2() {
<a name="line75"></a>    asyncTestCase.continueTesting();
<a name="line76"></a>    assertEquals(&#39;Did not wait for inner waitForAsync&#39;,
<a name="line77"></a>                 activeTest,
<a name="line78"></a>                 asyncTestCase.activeTest_);
<a name="line79"></a>  }
<a name="line80"></a>  asyncTestCase.waitForAsync(&#39;1&#39;);
<a name="line81"></a>  window.setTimeout(async1, 0);
<a name="line82"></a>}
<a name="line83"></a>function test3() {
<a name="line84"></a>  assertTrue(curTestIsDone);
<a name="line85"></a>  doAsyncStuff(2, &#39;test3&#39;);
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function tearDownPage() {
<a name="line89"></a>  debug(&#39;tearDownPage was called.&#39;);
<a name="line90"></a>  assertTrue(curTestIsDone);
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>var callback = function() {
<a name="line95"></a>  curTestIsDone = true;
<a name="line96"></a>  asyncTestCase.signal();
<a name="line97"></a>};
<a name="line98"></a>var doAsyncSignals = function() {
<a name="line99"></a>  curTestIsDone = false;
<a name="line100"></a>  window.setTimeout(callback, 0);
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>function testSignalsReturn() {
<a name="line104"></a>  doAsyncSignals();
<a name="line105"></a>  doAsyncSignals();
<a name="line106"></a>  doAsyncSignals();
<a name="line107"></a>  asyncTestCase.waitForSignals(3);
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testSignalsMixedSyncAndAsync() {
<a name="line111"></a>  asyncTestCase.signal();
<a name="line112"></a>  doAsyncSignals();
<a name="line113"></a>  doAsyncSignals();
<a name="line114"></a>  asyncTestCase.waitForSignals(3);
<a name="line115"></a>}
<a name="line116"></a>
<a name="line117"></a>function testSignalsMixedSyncAndAsyncMultipleWaits() {
<a name="line118"></a>  asyncTestCase.signal();
<a name="line119"></a>  doAsyncSignals();
<a name="line120"></a>  asyncTestCase.waitForSignals(1);
<a name="line121"></a>  doAsyncSignals();
<a name="line122"></a>  asyncTestCase.waitForSignals(2);
<a name="line123"></a>}
<a name="line124"></a>
<a name="line125"></a>function testSignalsCallContinueTestingBeforeFinishing() {
<a name="line126"></a>  doAsyncSignals();
<a name="line127"></a>  asyncTestCase.waitForSignals(2);
<a name="line128"></a>
<a name="line129"></a>  window.setTimeout(function() {
<a name="line130"></a>    var thrown = assertThrows(function() {
<a name="line131"></a>      asyncTestCase.continueTesting();
<a name="line132"></a>    });
<a name="line133"></a>    assertEquals(&#39;Still waiting for 1 signals.&#39;, thrown.message);
<a name="line134"></a>  }, 0);
<a name="line135"></a>  doAsyncSignals(); // To not timeout.
<a name="line136"></a>}
</pre>


</body>
</html>
